package com.oig.sys.workflow.app.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.oig.common.exception.BadRequestException;
import com.oig.common.util.JsonUtil;
import com.oig.sys.msg.constants.MsgEnums;
import com.oig.sys.workflow.app.IAppCronJobService;
import com.oig.sys.workflow.constants.JobEnums;
import com.oig.sys.workflow.dto.CronJobDto;
import com.oig.sys.workflow.service.ITbCronJobService;
import com.oig.sys.workflow.vo.CronJobVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class AppCronJobServiceImpl implements IAppCronJobService {

    private final ITbCronJobService cronJobService;


    @Override
    public CronJobVo addCronJob(CronJobDto cronJobDto) {
        String finishOpts=null;
        if (CollectionUtil.isNotEmpty(cronJobDto.getFinishOpts())){
            this.checkFinishOpts(cronJobDto);
            finishOpts = JsonUtil.toJSONString(cronJobDto.getFinishOpts());
        }
        return cronJobService.addCronJob(cronJobDto, finishOpts);
    }

    private void checkFinishOpts(CronJobDto cronJobDto){
        cronJobDto.getFinishOpts().forEach (op->{
            if (JobEnums.FinishOptType.WEB_HOOK.getCode().equals(op)){
                log.debug("has opt:{}", JobEnums.FinishOptType.WEB_HOOK.getName());
                if (StringUtils.isBlank(cronJobDto.getWebHook())){
                    throw new BadRequestException("勾选了调用webHook,web_hook地址必填") ;
                }
            } else if (JobEnums.FinishOptType.NOTIFY.getCode().equals(op)){
                if (StringUtils.isBlank(cronJobDto.getNotifyTitle())){
                    throw new BadRequestException("通知标题必填") ;
                }
                if (StringUtils.isBlank(cronJobDto.getNotifyContent())){
                    throw new BadRequestException("通知内容必填");
                }
                if (cronJobDto.getPushChannelId()==null){
                    throw new BadRequestException("渠道id必填");
                }
                if (StringUtils.isBlank(cronJobDto.getPushChannelName())){
                    throw new BadRequestException("渠道名称必填");
                }
                if (MsgEnums.MsgType.TARGET.getCode().equals(cronJobDto.getNotifyType())){	//定向通知
                    log.debug("定向通知");
                    if (StringUtils.isBlank(cronJobDto.getReceiverType())){
                        throw new BadRequestException("定向通知需指定接收方");
                    }
                    if (cronJobDto.getReceiverId()==null||StringUtils.isBlank(cronJobDto.getReceiverName())){
                        throw new BadRequestException("指定了接收方类型，必须指定接收方");
                    }
                }
            }
        });
    }

    @Override
    public CronJobVo updateCronJob(CronJobDto cronJobDto) {
        String finishOpts=null;
        if (CollectionUtil.isNotEmpty(cronJobDto.getFinishOpts())){
            log.debug("finish opts:{}", cronJobDto.getFinishOpts());
            this.checkFinishOpts(cronJobDto);
            finishOpts = JsonUtil.toJSONString(cronJobDto.getFinishOpts());
        }
        return cronJobService.updateCronJob(cronJobDto, finishOpts);
    }

    @Override
    public CronJobVo getCronJob(Integer id) {
        return cronJobService.getCronJob(id);
    }

    @Override
    public void cancelCronJob(Integer id) {
        this.cronJobService.cancelCronJob(id);
    }


    @Override
    public void disableJob(List<Integer> jobIdList) {
        this.cronJobService.disableJob(jobIdList);
    }

    @Override
    public List<CronJobVo> enableJob(List<Integer> jobIdList) {
        return this.cronJobService.enableJob(jobIdList);
    }
}
